﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BUS;
using DAL;
namespace QuanLyNhaSach
{
    public partial class Form_QLSach : DevComponents.DotNetBar.Office2007Form
    {
        Form_Them_CapNhat_Sach frm;
        Sach_BUS sach_bus;
        public Form_QLSach()
        {
            InitializeComponent();
        }

        private void Form_QLSach_Load(object sender, EventArgs e)
        {
            dtg_QLSach.AutoGenerateColumns = false;
            LoadDanhSachSach();
        }
        void LoadDanhSachSach()
        {
            sach_bus = new Sach_BUS();
            dtg_QLSach.DataSource = sach_bus.LayDanhSachSach();
        }
        void frm_KhiThemMoi()
        {
            LoadDanhSachSach();
        }
        void frm_KhiCapNhat()
        {
            LoadDanhSachSach();
        }
        int dong;
        private void dtg_QLSach_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            dong = e.RowIndex;
        }

        private void dtg_QLSach_Click(object sender, EventArgs e)
        {
            if (dtg_QLSach.Rows[dong].Cells["CHECKBOX"].Value != null)
                dtg_QLSach.Rows[dong].Cells["CHECKBOX"].Value = null;
            else
                dtg_QLSach.Rows[dong].Cells["CHECKBOX"].Value = true;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            frm = new Form_Them_CapNhat_Sach();
            frm.KhiThemMoi += frm_KhiThemMoi;
            frm.Action = "Insert";
            frm.ShowDialog();
        }
        List<string> MaSach;
        Sach_DTO sach_dto;
        private void btnXoa_Click(object sender, EventArgs e)
        {
            MaSach = new List<string>();
            for (int i = 0; i < dtg_QLSach.Rows.Count; i++)
            {
                if (dtg_QLSach.Rows[i].Cells["CHECKBOX"].Value != null)
                    MaSach.Add(dtg_QLSach.Rows[i].Cells["Str_MaSach"].Value.ToString());
            }
            if (MaSach.Count == 0)
            {
                MessageBoxEx.Show("Bạn chưa chọn sách để xóa.", "Thông báo");
                return;
            }

            if (MessageBoxEx.Show("Bạn có chắc chắn xóa dòng này không?", "Xóa sách", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                for (int i = 0; i < MaSach.Count; i++)
                {
                    sach_dto = new Sach_DTO();
                    sach_bus = new Sach_BUS();
                    sach_dto.Str_MaSach = MaSach[i].ToString();
                    sach_bus.XoaSach(sach_dto);
                }
                LoadDanhSachSach();
                MaSach.Clear();
            }
            else
            {
                for (int i = 0; i < dtg_QLSach.Rows.Count; i++)
                {
                    dtg_QLSach.Rows[i].Cells["CHECKBOX"].Value = false;
                }
            }
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            frm = new Form_Them_CapNhat_Sach();
            frm.KhiCapNhat += frm_KhiCapNhat;
            frm.Action = "Update";
            frm.ID = dtg_QLSach.Rows[dong].Cells["Str_MaSach"].Value.ToString();
            frm.ShowDialog();
        }

        private void dtg_QLSach_DoubleClick(object sender, EventArgs e)
        {
            btnCapNhat_Click(sender, e);
        }
    }
}
